package com.acoresoft.sm.web.controller;


import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.acoresoft.sm.entity.User;
import com.acoresoft.sm.service.UserService;

/**
 * 用户
 * @author WallenHeng
 * @date 2014年6月10日 下午1:46:00
 *
 */
@Controller
@RequestMapping("/user")
@SessionAttributes("userList")
public class UserController {
	private Log log=LogFactory.getLog(UserController.class);
	@Resource
	private UserService userService;
	

	/**
	 * UserList
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(){
		/**
		 * 想要在页面展现数据,必须返回ModelAndView类型,返回String是不能获取数据的
		 * */
		ModelAndView mv = new ModelAndView();
		List<User> users = userService.getUserList();				
		mv.addObject("userList",users);				
		mv.setViewName("user/list");			
		return mv;
	}
	
	
	
	/**
	 * 新增用户
	 * @param user
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(User user){
		log.debug("ADD USER: "+ user);	
		this.userService.addUser(user);		
		return list();
	}
	
	/**
	 * 跳转至添加用户
	 * @return
	 */
	@RequestMapping("/toAdd")
	public String toAdd(){		
		return "user/add";
	}
	
	/**
	 * 根据id删除用户
	 * @param id
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable("id") String id){
		this.userService.delUser(id);
		return list();
	}
	
	/**
	 * 根据id获取用户
	 * @param id
	 * @return
	 */
	@RequestMapping("/get/{id}")
	public ModelAndView getUser(@PathVariable("id") String id){
		User user = this.userService.getUserByUid(id);
		ModelAndView mv = new ModelAndView("user/edit");
		mv.addObject("user",user);
		return mv;
	}	
	
	/**
	 * 编辑用户
	 * @param user
	 * @return
	 */
	@RequestMapping("/update")
	public ModelAndView editUser(User user){	
		log.debug("编辑: "+user);
		this.userService.editUser(user);
		return list();
	}		
	
}
